<?php
class ModelSaleKeyword extends Model {
	
	public function addKeyword($data = array()) {
      	$this->db->query("INSERT INTO `" . DB_PREFIX . "key_words` SET category_id = '" . (int)$data['category_id']. 
      		"', language_id = '" . (int)$data['language_id'] . 
      		"', `keyword` = '" . $this->db->escape($data['keyword']) . 
      		"', click_num = '" . (int)$data['click_num'] . 
      		"', show_home = '" . (int)$data['show_home'] . 
      		"', `status` = '" . (int)$data['status'] . "'");
	}
	
	public function editKeyword($id, $data = array()) {
      	$this->db->query("UPDATE `" . DB_PREFIX . "key_words` SET category_id = '" . (int)$data['category_id']. 
      		"', language_id = '" . (int)$data['language_id'] . 
      		"', `keyword` = '" . $this->db->escape($data['keyword']) . 
      		"', click_num = '" . (int)$data['click_num'] . 
      		"', show_home = '" . (int)$data['show_home'] . 
      		"', `status` = '" . (int)$data['status'] . "' WHERE id = '" . (int)$id . "'"); 
	}

	public function getKeyword($id) {
		$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "key_words` WHERE id = '" . (int)$id . "'");
		return $query->row;
	}
	
	public function getKeywords($data = array()) {
		$language_id = null;
		if (isset($data['filter_language_id'])) {
			$language_id = (int)$data['filter_language_id'];
		}else{
			$language_id = (int)$this->config->get('config_language_id');
		}
		
		$sql = "SELECT k.*, c.`name` category_name, cd.display_name category_display_name, l.`name` language_name, l.image language_image FROM `" . 
			DB_PREFIX . "key_words` k LEFT JOIN " . 
			DB_PREFIX . "category c ON k.category_id = c.category_id LEFT JOIN " .  
			DB_PREFIX . "category_description cd ON c.category_id = cd.category_id AND cd.language_id = '" . $language_id . "' LEFT JOIN `" .  
			DB_PREFIX . "language` l ON k.language_id = l.language_id";

		$sql_params = '';
		if (isset($data['filter_category_id'])) {
			$sql_params .= " AND k.category_id = '" . (int)$data['filter_category_id'] . "'";
		}
		if (isset($data['filter_language_id'])) {
			$sql_params .= " AND k.language_id = '" . (int)$data['filter_language_id'] . "'";
		}
		if (isset($data['filter_keyword'])) {
			$sql_params .= " AND LCASE(k.`keyword`) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_keyword'])) . "%'";
		}
		if (isset($data['filter_show_home'])) {
			$sql_params .= " AND k.show_home = '" . $data['filter_show_home'] . "'";
		}
		if (isset($data['filter_status'])) {
			$sql_params .= " AND k.`status` = '" . (int)$data['filter_status'] . "'";
		}
		if($sql_params != ''){
			$sql_params = trim($sql_params);
			$sql .= " WHERE " . ltrim($sql_params, "AND");
		}
		
		$sort_data = array(
			'k.keyword',
			'k.click_num',
			'k.show_home',
			'k.status'
		);
		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $data['sort'];
		} else {
			$sql .= " ORDER BY k.click_num";
		}
		if (isset($data['order']) && ($data['order'] == 'DESC')) {
			$sql .= " DESC";
		} else {
			$sql .= " ASC";
		}
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}
			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}
		$query = $this->db->query($sql);
		return $query->rows;
	}
	
	public function getTotalKeywords($data = array()) {
      	$sql = "SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "key_words`";
		$sql_params = '';
		if (isset($data['filter_category_id'])) {
			$sql_params .= " AND category_id = '" . (int)$data['filter_category_id'] . "'";
		}
		if (isset($data['filter_language_id'])) {
			$sql_params .= " AND language_id = '" . (int)$data['filter_language_id'] . "'";
		}
		if (isset($data['filter_keyword'])) {
			$sql_params .= " AND LCASE(`keyword`) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_keyword'])) . "%'";
		}
		if (isset($data['filter_show_home'])) {
			$sql_params .= " AND show_home = '" . $data['filter_show_home'] . "'";
		}
		if (isset($data['filter_status'])) {
			$sql_params .= " AND `status` = '" . (int)$data['filter_status'] . "'";
		}
		if($sql_params != ''){
			$sql_params = trim($sql_params);
			$sql .= " WHERE " . ltrim($sql_params, "AND");
		}
		$query = $this->db->query($sql);
		return $query->row['total'];
	}
}
?>